package cn.edu.sdjzu.xg23.kcsj01_backend.controller;

import cn.edu.sdjzu.xg23.kcsj01_backend.controller.*;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.LiteratureDatabase;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.*;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.*;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.*;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.*;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class PaperController {
    @Autowired
    private PaperService paperService;
    @PreAuthorize("hasAuthority('paper:search:all') or hasAuthority('paper:search:my')")
    @PostMapping("/papers/search")
    public Result get(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody (required = false)Paper paper) {

        PageInfo<Paper> paperPageInfo = paperService.get(pageNum,pageSize,paper);
        System.out.println(paperPageInfo);
        return Result.success(paperPageInfo);
    }
    @PreAuthorize("hasAuthority('paper:add')")
    @PostMapping("paper")
    public Result save(@RequestBody Paper paper) {
        return Result.success(paperService.save(paper));
    }
    @PreAuthorize("hasAuthority('paper:update')")
    @PutMapping("paper")
    public Result update(@RequestBody Paper paper) {
        return Result.success(paperService.update(paper));
    }
    @PreAuthorize("hasAuthority('paper:delete')")
    @DeleteMapping("paper/{id}")
    public Result delete(@PathVariable long id) {
        return Result.success(paperService.delete(id));
    }

    @PreAuthorize("hasAuthority('paper:approve')")
    @PutMapping("papers/approve")
    public Result approvePatent(@RequestBody Paper paper) {
        if (paperService.approvePaper(paper)) {
            return Result.success("审批成功");
        } else {
            return Result.error("500", "审批失败");
        }
    }

}